<?php

class DefaultController extends MyAdminController
{
	public $modelName = 'SeoSettings';
	public $map = array(
			'faq-default-index' => 'List FAQ',
			'faq-default-view' => 'View FAQ page',
			'infopages-default-view' => 'View infopage',
			'news-default-index' => 'List news',
			'news-default-view' => 'View news page',
	);
	
	public function actionRobotstxt()
	{
		$model = new RobotstxtForm();
		$model->setAttributes(array('content'=>file_get_contents(Yii::getPathOfAlias('webroot').'/robots.txt')));
		
		if(isset($_POST['RobotstxtForm']))
		{
			$model->attributes=$_POST['RobotstxtForm'];
			if ($model->validate())
			{
				file_put_contents(Yii::getPathOfAlias('webroot').'/robots.txt', $model->attributes['content']);
			}
		}
		$this->render('index', array('model'=>$model, 'template'=>'_robotstxt', 'params'=>array()));
	}
	
	public function actionAnalytics()
	{
		if (!$settings = Settings::model()->get('analytics'))
		{
			$settings = array();
		}

		$data = array();
		foreach ($settings as $key=>$value)
		{
			$data[] = array('placeholder' => $key);
		}

		$sort = new CSort();
		$sort->defaultOrder = 'placeholder ASC';
		$sort->attributes = array('placeholder');
		
		$dataProvider = new CArrayDataProvider($data, array(
			'pagination'=>array(
				'pageSize'=>param('adminPaginationPageSize', 20),
			),
			'sort' => $sort
		));
		
		$this->render('index', array('model'=>$model, 'template'=>'_analytics', 'params'=>array('dataProvider'=>$dataProvider)));
	}
	
	public function actionUpdateanalytics()
	{
		$placeholder = CHttpRequest::getParam('placeholder');

		$model = new AnalyticsForm();
		if (!empty($placeholder))
		{
			$model->setAttributes(array(
				'placeholder'=>$placeholder, 
				'content'=>Settings::model()->get('analytics', $placeholder)
			));
		}
		
		if(isset($_POST['AnalyticsForm']))
		{
			$model->attributes = $_POST['AnalyticsForm'];
			if ($model->validate())
			{
				Settings::model()->set('analytics', $model->attributes['placeholder'], $model->attributes['content']);
				
				$this->redirect(array('analytics'));
			}
		}
		
		$this->render('updateanalytics', array('model'=>$model));
	}
	
	public function actionDeleteanalytics($placeholder)
	{
		if(Yii::app()->request->isPostRequest)
		{
			Settings::model()->get('analytics');
			Settings::model()->delete('analytics', $placeholder);
			
			Yii::app()->user->setFlash('success', tt('Item successfully deleted'));
			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}
	
	public function filterDefaultSetting($haystack, $needle)
	{
		foreach ($haystack as $h)
		{
			if ($h['pageId'] == $needle)
			{
				return $h;
			}
		}
		return false;
	}
	
	public function actionIndex()
	{
		$default = SeoSettings::model()->getDefaultSettings();
		
		$pages = array();
		$languages = Languages::model()->getLanguageCodes();
		foreach ($languages as $lang)
		{
			foreach ($this->map as $route=>$desc)
			{
				$pageId = $route.'-'.$lang;
				$values = call_user_func(array($this, 'filterDefaultSetting'), $default, $pageId);
				$pages[] = array(
					'pageId'=>$pageId,
					'keywords' => ($values)?$values['keywords']:'',
					'description' => ($values)?$values['description']:'',
					'defaultPage' => tt($desc),
					'defaultPageId' => str_replace('/', '-', $route),
				);
			}
		}
		
		$sort = new CSort();
		$sort->defaultOrder = 'defaultPageId ASC';
		$sort->attributes = array('defaultPageId');
		
		$dataProvider = new CArrayDataProvider($pages, array(
			'pagination'=>array(
				'pageSize'=>param('adminPaginationPageSize', 20),
			),
			'sort' => $sort
		));
		
		$this->render('index', array('model'=>$model, 'template'=>'_keywords', 'params'=>array('dataProvider'=>$dataProvider)));
	}
	
	public function loadModel($pageId = null)
	{
		$model = new $this->modelName;

		if($pageId !== null){
			if($this->with){
				$model = $model->resetScope()->with($this->with)->findByPk($pageId);
			}
			else{
				$model = $model->resetScope()->findByPk($pageId);
			}
		}
		if($this->scenario){
			$model->scenario = $this->scenario;
		}

		if($model===null)
		{
			$model = new $this->modelName;
			$model->setAttribute('pageId', $pageId);
		}
		return $model;
	}

	public function actionEdit($pageId)
	{
		preg_match("/^(.*)-(.*)$/", $pageId, $matches);
		$h1 = tt($this->map[$matches[1]]);
		
		$model = $this->loadModel($pageId);

		if(isset($_POST[$this->modelName]))
		{
			$model->attributes = $_POST[$this->modelName];
			$model->setAttribute('defaultId', '1');
			if($model->save())
			{
				Yii::app()->user->setFlash('success', tt('Seo setting successfully updated'));
				$this->redirect(array('index'));
			}
		}

		$this->render('edit',array('model'=>$model, 'h1'=>$h1));
	}

	public function actionAjaxSaveMetaInformation()
	{
		$model = new SeoSettings();
		if (isset($_POST['SeoSettings']))
		{
			$model->attributes=$_POST['SeoSettings'];
			if($model->save()) 
			{
				echo tt('Seo settings successfully saved');
			}
		}
	}
}